package com.meitu.community.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.meitu.community.model.User;

public class JDBCUtils {

    private static DataSource dataSource = null;

    static {
        dataSource = new ComboPooledDataSource("meituCommunity");
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("数据库连接异常");
        }
    }

    public static void release(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("");
        }
    }

    public static void main(String[] args) throws Exception {
        Connection connection = getConnection();
        String sql = "select * from user";
        ResultSet rs = connection.prepareStatement(sql).executeQuery();
        while (rs.next()) {
            System.out.print(rs.getString("username") + " ");
            System.out.print(rs.getString("password") + " ");
        }
        System.out.println();
    }

}
